package com.yun.aop;


import cn.hutool.core.util.ReflectUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;


import java.util.Date;

/**
 * @ fileName:EntityEnhacerAspect
 * @ description:
 * @ author:zyk
 * @ createTime:2021/12/15 11:20
 * @ version:1.0.0
 */
@Slf4j
@EnableAspectJAutoProxy
@Component
@Aspect
public class EntityEnhacerAspect {
    /**
     * @ author: zyk
     * @ description:定义一个切点
     * @ date: 2021/12/15 11:27
     * @ param: []
     * @ return: void
     */
    @Pointcut("execution(* com.yun.controller.*.insert*(..))")
    public void saveMethod(){
    }
    @Pointcut("execution(* com.yun.controller.*.update*(..))")
    public void updateMethod(){
    }
    /**
     * @ author: zyk
     * @ description:前置增强
     * @ date: 2021/12/15 11:49
     * @ param: [joinPoint]
     * @ return: void
     */
    @Before("saveMethod()")
    public void enhancer(JoinPoint joinPoint){
        log.debug("前置方法开始执行"+new Date());
        //获取参数
        Object[]args = joinPoint.getArgs();

        //获取方法名
        String name = joinPoint.getSignature().getName();

        //修改参数
        Object arg1=null;
        if (args!=null&&args.length>0){
            arg1=args[0];
        }
        //增强处理第一个参数（实体类）
        ReflectUtil.invoke(arg1,"setCreateBy","zyk");
        ReflectUtil.invoke(arg1,"setCreateTime",new Date());
    }

    @Before("updateMethod()")
    public void enhancer1(JoinPoint joinPoint){
        log.debug("前置方法开始执行"+new Date());
        //获取参数
        Object[]args = joinPoint.getArgs();

        //获取方法名
        String name = joinPoint.getSignature().getName();

        //修改参数
        Object arg1=null;
        if (args!=null&&args.length>0){
            arg1=args[0];
        }
        //增强处理第一个参数（实体类）
        ReflectUtil.invoke(arg1,"setUpdateBy","zyk");
        ReflectUtil.invoke(arg1,"setUpdateTime",new Date());
    }
}
